<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="root" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>发布公告</title>
<link href="${root}/static/css/bootstrap.min.css" rel="stylesheet"
	type="text/css" />
<script type="text/javascript" src="${root}/static/js/jquery-1.12.4.js"></script>
<script type="text/javascript" src="${root}/static/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${root}/static/js/vue.min.js"></script>
<script type="text/javascript" src="${root}/static/js/axios.min.js"></script>
<script type="text/javascript"
	src="${root}/static/js/My97DatePicker/WdatePicker.js"></script>
</head>
<body>
	<div id="app" class="container-fluid">
		<div class="col-md-2" style="margin-top: 20px">
			<button class="btn btn-info btn-lg" @click="add()">发布新公告</button>
		</div>
		<table class="table">
			<caption>公告列表</caption>
			<thead>
				<tr>
					<th>编号</th>
					<th>内容</th>
					<th>开始时间</th>
					<th>结束时间</th>
					<th>状态</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<tr v-for="item in pager.items">
					<td>{{item.nsid}}</td>
					<td>{{item.nsinfo}}</td>
					<td>{{item.nsbeginS}}</td>
					<td>{{item.nsstopS}}</td>
					<td>{{item.nowTimeS}}</td>
					<td><a href="javascript:void(0);" @click="remove(item.nsid)"><button
								class="btn btn-info">删除</button></a></td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="6">
						<ul class="pagination">
							<li><a href="javascript:void(0);" @click="list(1)">&laquo;</a></li>
							<li><a href="javascript:void(0);"
								@click="list(pager.begin-pager.step)">&larr;</a></li>
						</ul>
						<ul class="pagination" v-for="n in pager.end+1-pager.begin">
							<li :class="pager.begin+n-1==pager.index?'active':''"><a
								href="javascript:void(0);" @click="list(pager.begin+n-1)">{{pager.begin+n-1}}</a>
							</li>
						</ul>
						<ul class="pagination">
							<li><a href="javascript:void(0);"
								@click="list(pager.begin+pager.step)">&rarr;</a></li>
							<li><a href="javascript:void(0);" @click="list(pager.page)">&raquo;</a></li>
						</ul>
					</td>
				</tr>
			</tfoot>
		</table>
		<div id="mymodal" class="modal">
			<div class="modal-dialog" data-backdrop="static">
				<div class="modal-content">
					<div class="modal-header">
						<button class="close" data-dismiss="modal">&times;</button>
						<h4>发布公告</h4>
					</div>
					<div class="modal-body">
						<form class="form-horizontal" @submit.prevent method="post">
							<div class="form-group">
								<label class="col-sm-2 control-label text-right">公告内容:</label>
								<div class="col-sm-4">
									<textarea id="nsinfo" rows="5" cols="10" class="form-control"
										required></textarea>
								</div>
								<p class="col-sm-6 help-block">公告内容不能为空</p>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label text-right">开始时间:</label>
								<div class="col-sm-4">
									<!-- <input type="text" id="nsbegin"
										onFocus="WdatePicker({readOnly:true,minDate:'%y-%M-%d {%H+1}:%m:%s',dateFmt:'yyyy-MM-dd HH:mm:ss'})"
										class="form-control" required placeholder="请输入开始时间" /> -->
									<input type="text" id="nsbegin"
										onFocus="WdatePicker({readOnly:true,minDate:'%y-%M-%d %H:%m:%s',dateFmt:'yyyy-MM-dd HH:mm:ss'})"
										class="form-control" required placeholder="请输入开始时间" />
								</div>
								<p class="col-sm-6 help-block">开始时间不能为空</p>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label text-right">结束时间:</label>
								<div class="col-sm-4">
									<input type="text" id="nsstop"
										onFocus="WdatePicker({readOnly:true,minDate:'%y-%M-%d {%H+2}:%m:%s',maxDate:'%y-%M-{%d+7}',dateFmt:'yyyy-MM-dd HH:mm:ss'})"
										class="form-control" required placeholder="请输入结束时间" />
								</div>
								<p class="col-sm-6 help-block">结束时间不能为空</p>
							</div>
							<div class="form-group">
								<div class="col-sm-4 col-sm-offset-2">
									<input type="submit" class="btn btn-default" value="确定"
										@click="save()" /> <input type="button" data-dismiss="modal"
										class="btn btn-default" value="取消" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		new Vue({
			el : '#app',
			data : {
				index : 1,
				size : 5,
				step : 10,
				pager : {}
			},
			methods : {
				list : function(index) {
					var self = this;
					self.index = index;
					var url = "${root}/manage/news/newslist?index="
							+ self.index + "&size=" + self.size + "&step="
							+ self.step;
					axios.get(url).then(function(res) {
						if(res.data.code=="500"){
							alert(res.data.message);
						}else{
							self.pager = res.data;
						}
					});
				},
				add : function() {
					$("#nsinfo").val("");
					$("#nsbegin").val("");
					$("#nsstop").val("");
					$("#mymodal").modal("show");
				},
				save : function() {
					var self = this;
					var count = 0;
					var nsinfo = document.getElementById("nsinfo");
					var nsbegin = document.getElementById("nsbegin");
					var nsstop = document.getElementById("nsstop");

					if (nsinfo.validity.valueMissing) {
						nsinfo.setCustomValidity("公告不能为空");
					} else {
						nsinfo.setCustomValidity("");
						count++;
						if (nsbegin.value.length == 0) {
							alert("开始时间不能为空");
						} else {
							count++;
							if (nsstop.value.length == 0) {
								alert("结束时间不能为空");
							} else {
								count++;
							}
						}
					}

					if (count == 3) {
						var url = "${root}/manage/news/add";
						var fd = new FormData();
						fd.append("nsinfo", nsinfo.value);
						fd.append("nsbegin", nsbegin.value);
						fd.append("nsstop", nsstop.value);
						axios.post(url, fd).then(function(res) {
							alert(res.data.message);
							$("#mymodal").modal("hide");
							self.list(1);
						});
					}
				},
				remove : function(id) {
					var self = this;
					if (confirm("是否确认要删除此公告？")) {
						var url = "${root}/manage/news/remove?nsid=" + id;
						axios.get(url).then(function(res) {
							alert(res.data.message);
							self.list(1);
						});
					}
				},
			},
			mounted : function() {
				this.list(this.index);
			}
		})
	</script>
</body>
</html>